package com.guoxing.touchdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Description:
 * Copyright(c) 2016,邱国兴
 * This Program is protected by copyright laws
 * Created by Little Monster on 2016/12/1.
 *
 * @author 邱国兴
 */

public class TouchView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

    private Bitmap bitmap;
    private Matrix matrix;
    private float lastDis;
    private float lastDegrees;
    private GestureDetector detector;
    private boolean isDouble;

    public TouchView(Context context) {
        this(context, null);
    }

    public TouchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        matrix = new Matrix();
        detector = new GestureDetector(context, this);
        detector.setOnDoubleTapListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, matrix, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("调试", "onTouchEvent: ");
        if (event.getPointerCount() == 2) {
            Log.d("调试", "onTouchEvent: 双指");
            isDouble = true;
            float x1 = event.getX(0);
            float x2 = event.getX(1);
            float y1 = event.getY(0);
            float y2 = event.getY(1);
            float cx = (x1 + x2) / 2;
            float cy = (y1 + y2) / 2;
            float dis = (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
            float degrees = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    lastDis = dis;
                    lastDegrees = degrees;
                    break;
                case MotionEvent.ACTION_MOVE:
                    matrix.postScale(dis / lastDis, dis / lastDis, cx, cy);
                    matrix.postRotate(degrees - lastDegrees, cx, cy);
                    lastDis = dis;
                    lastDegrees = degrees;
                    break;
            }
        } else if (event.getPointerCount() == 1) {
            Log.d("调试", "onTouchEvent: 单指");
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                isDouble = false;
            }
            if (!isDouble) {
                return detector.onTouchEvent(event);
            }
        }
        invalidate();
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        matrix.postTranslate(-distanceX, -distanceY);
        invalidate();
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        matrix.postScale(1.2f, 1.2f, e.getX(), e.getY());
        invalidate();
        return false;
    }
}
